import pymysql

class InsertUtils:
    def __init__(self, host, user, password, database, port=3306, charset='utf8'):
        self.host = host
        self.user = user
        self.password = password
        self.database = database
        self.port = port
        self.charset = charset

    def execute_sql(self, sql, data=None):
        db = pymysql.connect(
            host=self.host,
            user=self.user,
            password=self.password,
            database=self.database,
            port=self.port,
            charset=self.charset
        )
        try:
            with db.cursor() as cursor:
                cursor.executemany(sql, data)
                db.commit()
                print("数据操作成功")
        except Exception as e:
            print("数据操作失败：", e)
        finally:
            db.close()

if __name__ == '__main__':
    db = InsertUtils('127.0.0.1', 'root', '123456', 'test')
    data = (('张三', 18, '男', '1.73'),
            ('李四', 18, '男', '1.78'),
            ('小一', 22, '女', '1.68'),
            ('小八', 24, '男', '1.75'))

    sql = "insert into person values (%s,%s,%s,%s)"
    db.execute_sql(sql, data)